<template>
    <a-modal :open="visible" title="审批" width="600px" forceRender :destroyOnClose="true" :maskClosable="false"
        @cancel="onClose">
        <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }">
            <a-form-item label="审批结果" name="result">
                <a-select v-model:value="form.result" @change="handleChange" :allowClear="true" size="Middle"
                    style="width: 100%" placeholder="请选择审批结果">
                    <a-select-option :value="1">审批通过</a-select-option>
                    <a-select-option :value="0">审批不通过</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="审批意见" name="blOpinion">
                <a-textarea v-model:value.trim="form.blOpinion" placeholder="请输入审批意见"
                    :auto-size="{ minRows: 3, maxRows: 5 }" />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-space>
                <a-button @click="onClose">取消</a-button>
                <a-button type="primary" v-throttle @click="onSubmit">提交</a-button>
            </a-space>
        </template>
    </a-modal>
</template>

<script setup>
    import { message } from 'ant-design-vue';
    import pcaa from 'china-area-data';
    import _ from 'lodash';
    import { nextTick, reactive, ref, defineProps, computed } from 'vue';
    import { ProManageApi } from '/@/api/ProManage/ProManage-api';
    import AreaCascader from '/@/components/framework/area-cascader/index.vue';
    import { SmartLoading } from '/@/components/framework/smart-loading';
    import Upload from '/@/components/support/file-upload/index.vue';
    import { regular } from '/@/constants/regular-const';
    import { smartSentry } from '/@/lib/smart-sentry';
    import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';
    import moment from "moment"; //日期转化
    import { useUserStore } from '/@/store/modules/system/user';
    defineExpose({
        showModal,
    });

    const emit = defineEmits(['refresh']);
    // ====== 标题===============
    const title = ref("");

    // --------------------- modal 显示与隐藏 ---------------------
    // 是否展示
    const visible = ref(false);

    const passData = ref({});
    function showModal(data) {
        passData.value = data;// 传值
        visible.value = true;
        nextTick(() => {
            formRef.value.clearValidate();
        });
    }


    // --------------------- form 表单 ---------------------

    // 关闭弹框
    function onClose() {
        Object.assign(form, formDefault)
        visible.value = false;
    }
    // 邮箱正则
    const regEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;

    // --------------------- 表单 ---------------------

    //  组件
    const formRef = ref();

    const formDefault = {
        result: undefined,//
        blOpinion: undefined,//描述 说明
        blclosureid: undefined,//id
    };
    let form = reactive({ ...formDefault });
    const rules = {
        result: [{ required: false, message: '请选择审核结果' }],
        blOpinion: [{ required: false, message: '请输入审核意见' }],
    };
    function handleChange(e) {
        form.status = e
        console.log('e:', e)
    }
    const blsysuserid = ref('');
    blsysuserid.value = computed(() => useUserStore().blsysuserid);
    function onSubmit() {
        formRef.value
            .validate()
            .then(async () => {
                if (form.result === '' || form.result === undefined || form.result === null) {
                    message.error("请选择审核结果")
                } else {
                    SmartLoading.show();
                    try {
                        console.log("dqwdwd");
                        const isPass = passData.value.blstate
                        console.log('isPass:', isPass)
                        form.blext1 = blsysuserid.value
                        form.blprojectname = passData.value.blprojectname
                        form.blprojectnum = passData.value.blext2
                        if (isPass == 0) {//基金科普审批
                            form.blauditopinionj = form.blOpinion
                            form.blstate = form.result
                            form.blclosureid = passData.value.blclosureid
                            delete form.blOpinion;//
                            delete form.result;//
                            if (form.status == 1) {//审批通过
                                form.blstate = -1
                                delete form.status;
                                console.log('form:', form)
                                await ProManageApi.updateStatus(form);
                                message.success(`提交成功`);
                                emit('refresh');
                                onClose();
                            } else {//审批不通过
                                if (form.blauditopinionj == undefined) {
                                    form.result = form.blstate
                                    message.error("请输入审核意见")
                                } else {
                                    delete form.status;
                                    form.blstate = -5
                                    await ProManageApi.updateStatus(form);
                                    message.success(`提交成功`);
                                    emit('refresh');
                                    onClose();
                                }
                            }
                        } else if (isPass == -1 || isPass == -5) {//技术科审批财务科审批
                            form.blauditopinionc = form.blOpinion
                            form.blstate = form.result
                            form.blclosureid = passData.value.blclosureid
                            delete form.blOpinion;//
                            delete form.result;//
                            if (form.status == 1) {//审批通过
                                form.blstate = -2
                                delete form.status;
                                await ProManageApi.updateStatus(form);
                                message.success(`提交成功`);
                                emit('refresh');
                                onClose();
                            } else {//审批不通过
                                if (form.blauditopinionc == undefined) {
                                    form.result = form.blstate
                                    message.error("请输入审核意见")
                                } else {
                                    delete form.status;
                                    form.blstate = 5
                                    await ProManageApi.updateStatus(form);
                                    message.success(`提交成功`);
                                    emit('refresh');
                                    onClose();
                                }
                            }

                        } else if (isPass == -2) {//副主任审批
                            form.blauditopinionf = form.blOpinion
                            form.blstate = form.result
                            form.blclosureid = passData.value.blclosureid
                            delete form.blOpinion;//
                            delete form.result;//
                            if (form.status == 1) {//审批通过
                                form.blstate = -3
                                delete form.status;
                                await ProManageApi.updateStatus(form);
                                message.success(`提交成功`);
                                emit('refresh');
                                onClose();
                            } else {//审批不通过
                                if (form.blauditopinionf == undefined) {
                                    form.result = form.blstate
                                    message.error("请输入审核意见")
                                } else {
                                    delete form.status;
                                    form.blstate = 5
                                    await ProManageApi.updateStatus(form);
                                    message.success(`提交成功`);
                                    emit('refresh');
                                    onClose();
                                }
                            }

                        } else if (isPass == -3) {//主任审批
                            form.blauditopinionz = form.blOpinion
                            form.blstate = form.result
                            form.blclosureid = passData.value.blclosureid
                            delete form.blOpinion;//
                            delete form.result;//
                            if (form.status == 1) {//审批通过
                                form.blstate = -4
                                delete form.status;
                                await ProManageApi.updateStatus(form);
                                message.success(`提交成功`);
                                emit('refresh');
                                onClose();
                            } else {//审批不通过
                                if (form.blauditopinionz == undefined) {
                                    form.result = form.blstate
                                    message.error("请输入审核意见")
                                } else {
                                    delete form.status;
                                    form.blstate = 5
                                    await ProManageApi.updateStatus(form);
                                    message.success(`提交成功`);
                                    emit('refresh');
                                    onClose();
                                }
                            }

                        } else if (isPass == -4) {//秘书长审批
                            form.blauditopinionm = form.blOpinion
                            form.blstate = form.result
                            form.blclosureid = passData.value.blclosureid
                            delete form.blOpinion;//
                            delete form.result;//
                            if (form.status == 1) {//审批通过
                                form.blstate = 2
                                delete form.status;
                                await ProManageApi.updateStatus(form);
                                message.success(`提交成功`);
                                emit('refresh');
                                onClose();
                            } else {//审批不通过
                                if (form.blauditopinionm == undefined) {
                                    form.result = form.blstate
                                    message.error("请输入审核意见")
                                } else {
                                    delete form.status;
                                    form.blstate = 5
                                    await ProManageApi.updateStatus(form);
                                    message.success(`提交成功`);
                                    emit('refresh');
                                    onClose();
                                }
                            }
                        }
                    } catch (error) {
                        smartSentry.captureError(error);
                    } finally {
                        SmartLoading.hide();
                    }
                }
            })
            .catch((error) => {
                message.error('参数验证错误，请仔细填写表单数据!');
            });
    }

</script>

<style lang="less" scoped>
    .form-width {
        width: 100% !important;
    }

    .footer {
        width: 100%;
        display: flex;
        justify-content: flex-end;
    }

    :deep(.ant-card-body) {
        padding: 10px;
    }
</style>